//
//  LoginViewController.m
//  Tang
//
//  Created by 邓芮 on 15/12/10.
//  Copyright © 2015年 352324497@qq.com. All rights reserved.
//

#import "LoginViewController.h"
#import "UserProtocolController.h"
#import "MyNavController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *user;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)Login:(id)sender
{
    [BmobUser loginWithUsernameInBackground:self.user.text password:self.password.text block:^(BmobUser *user, NSError *error) {
        if (error)
        {
            if (self.user.text.length == 0)
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
                [alert addAction:sure];
                [self presentViewController:alert animated:NO completion:nil];
            }
            else if (![self.user.text isEqualToString:user.username])
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"用户名错误" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
                [alert addAction:sure];
                [self presentViewController:alert animated:NO completion:nil];
            }
            else if (self.password.text.length == 0)
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
                [alert addAction:sure];
                [self presentViewController:alert animated:NO completion:nil];
            }
            else if (![self.password.text isEqualToString:user.password])
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"密码错误" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
                [alert addAction:sure];
                [self presentViewController:alert animated:NO completion:nil];
            }

            return;
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}
- (IBAction)cancleAction:(UIButton *)sender
{
    self.navigationController.navigationBar.translucent = NO;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.user resignFirstResponder];
    [self.password resignFirstResponder];
}
- (IBAction)protolVC:(UIButton *)sender
{
    
    UserProtocolController * userVc = [[UserProtocolController alloc] init];
    MyNavController *mayNav = [[MyNavController alloc] initWithRootViewController:userVc];
    [self presentViewController:mayNav animated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
